import api from "../../../utils/api/api"
import storage from '../../../utils/storage'

Page({
  data: {
    date: "2021-09-01",
    time: "12:01",
    files: "",
    UUID: "",
    isChose: false,
    model: "",
    price: "",
    name: "",
    Image: "",
    tempFilePaths: "",
    url: "",
    isUpload: false,
    baseURL: "https://api.cpoe.top"
  },
  onLoad: function () {
    api.getDevice().then(res => {
      console.log(res);
    })
    let list_info = storage.getDeviceInfo()
    let temp = new Date()
    let time = temp.toLocaleTimeString()
    this.setData({
      time,
    })
    if (list_info) {
      this.setData({
        name: list_info.name,
        price: list_info.price,
        model: list_info.model
      })
    }
    console.log(this.data.time);
  },
  submit: function (e) {
    console.log(e);
    let url = "";
    let that = this;
    this.data.UUID = e.detail.value.UUID
    this.data.name = e.detail.value.name
    this.data.price = e.detail.value.price
    this.data.model = e.detail.value.model
    this.data.time = e.detail.value.time
    this.data.date = e.detail.value.date
    this.setData(this.data)
    api.registerDevice(e.detail.value.UUID, e.detail.value.name, e.detail.value.model).then(res => {
      console.log(res);
      that.data.url = 'https://api.cpoe.top/api/image/uploadImage/'
      that.setData(that.data)
      that.uploadImage(that.data.url)
      this.data.UUID = ""
      this.setData(this.data)
      wx.showToast({
        title: '数据提交成功',
        icon: "success"
      })
      
    })
    storage.setDeviceInfo(e.detail.value)

  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  next: function (e) {
    let that = this
    // that.data.tempFilePaths = ""
    that.data.isUpload = false
    that.data.UUID = ""
    that.setData(that.data)
  },
  scanCode: function (e) {
    wx.scanCode({
      onlyFromCamera: true,
      success: res => {
        console.log(res)
        this.setData({
          UUID: res.result
        })
      }
    })
  },
  chooseImageLongTap: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let Image = res.tempFilePaths[0]
        that.setData({
          tempFilePaths,
          Image: Image,
          isChose: true
        })
        console.log(that.data.Image);
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
      }
    })
  },
  chooseImage: function (e) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let Image = res.tempFilePaths[0]
        that.setData({
          tempFilePaths,
          Image: Image,
          isChose: true
        })
        console.log(that.data.Image);
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
      }
    })
  },
  uploadImage: function (url) {
    let that = this
    console.log(this.data, "~~~~~~~~")
    wx.uploadFile({
      url: url,
      filePath: this.data.tempFilePaths[0],
      name: 'file',
      formData: {
        ID: this.data.UUID,
        name: this.data.name
      },
      success(res) {
        console.log(res);
        if (res.statusCode == 200) {
          const data = JSON.parse(res.data)
          that.data.Image = that.data.baseURL + data.picture
          that.data.isUpload = true
          that.setData(that.data)
          console.log(that.data.Image);
          wx.showToast({
            title: '图片提交成功',
            icon: "success"
          })
        }
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  // previewImage: function (e) {
  //   wx.previewImage({
  //     current: e.currentTarget.id, // 当前显示图片的http链接
  //     urls: this.data.files // 需要预览的图片http链接列表
  //   })
  // },
})